<!-- 工资结算编辑弹窗 -->
<template>
  <div class="ele-body">
    <a-card :title="salarySettlementModalApp.title" :bordered="false">
      <a-form
        ref="form2"
        :model="salarySettlementModalApp.salarySettlement"
        :rules="salarySettlementModalApp.rules"
        :label-col="{md: {span: 7}, sm: {span: 24}}"
        :wrapper-col="{md: {span: 17}, sm: {span: 24}}">
        <a-row :gutter="16">

          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item label="员工:" name="staffId">
              <m-entity-select
                :default-value="salarySettlementModalApp.salarySettlement.staffName"
                v-model:value="salarySettlementModalApp.salarySettlement.staffId"
                placeholder="请选择员工"
                @change="calculateSalary"
                module="staff"
                entity="Staff"
                class="ele-fluid"
              />
            </a-form-item>
          </a-col>



          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item label="结算开始日期:" name="settleStartDate">
              <a-date-picker
                v-model:value="salarySettlementModalApp.salarySettlement.settleStartDate"
                placeholder="请选择结算开始日期"
                @change="calculateSalary"
                value-format="YYYY-MM-DD H:m:s"
                class="ele-fluid"/>
            </a-form-item>
          </a-col>

          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item label="结算日期:" name="settleEndDate">
              <a-date-picker
                v-model:value="salarySettlementModalApp.salarySettlement.settleEndDate"
                placeholder="请选择结算日期"
                @change="calculateSalary"
                value-format="YYYY-MM-DD H:m:s"
                class="ele-fluid"/>
            </a-form-item>
          </a-col>
          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item label="工资金额:" name="amount">
              <a-input-number
                v-model:value="salarySettlementModalApp.salarySettlement.amount"
                placeholder="请输入工资金额"

                :min="0"
                :step="1"
                class="ele-fluid"
              ></a-input-number>
            </a-form-item>
          </a-col>

          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item label="状态:" name="status">
              <a-input
                v-model:value="salarySettlementModalApp.salarySettlement.status"
                placeholder="请输入状态"
                :maxlength="255"
                allow-clear
              />
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="24" :xs="24">
            <a-form-item :wrapper-col="{md: {offset: 6}}" style="margin-bottom: -20px">
              <!--class="ele-pull-right"-->
              <div v-if="salarySettlementModalApp.display">
                <teleport to="#salarySettlementEditModalFooter">
                  <div>
                    <a-space size="middle">
                      <a-button @click="salarySettlementListApp.editModalShowing=false">取消
                      </a-button>
                      <a-button
                        type="primary"
                        @click="onSubmit(salarySettlementModalApp.salarySettlement)"
                        :loading="loading">提交
                      </a-button>
                      <!--<a-button v-if="!eid"
                                type="dashed"
                                @click="continueSubmit"
                                :loading="loading">继续提交
                      </a-button>-->
                    </a-space>
                  </div>
                </teleport>
              </div>
              <div v-if="!salarySettlementModalApp.display">
                <a-space size="middle">
                  <a-button @click="onBack()">返回列表</a-button>
                  <a-button
                    type="primary"
                    @click="onSubmit(salarySettlementModalApp.salarySettlement)"
                    :loading="loading">提交
                  </a-button>
                  <!--<a-button v-if="!eid"
                            type="dashed"
                            @click="continueSubmit"
                            :loading="loading">继续提交
                  </a-button>-->
                </a-space>
              </div>

            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-card>

  </div>

</template>

<script>
import {defineComponent, inject, reactive, onMounted} from 'vue'
import {useRoute, useRouter} from "vue-router"
import {SalarySettlementService} from "@/views/restaurant/staff/salarySettlement/salarySettlementService";
import {VXETable} from 'vxe-table'
import {useStore} from "vuex";
import regions from 'ele-admin-pro/packages/regions.js';


import MEntitySelect from "@/components/MEntity/entitySelect";

export default defineComponent({
  components: {
    MEntitySelect,
  },
  setup() {
    // 省市区数据
    const cityData = regions
    const route = useRoute();
    const router = useRouter();
    const store = useStore();
    let routeId = route.params.id;
    let salarySettlementListApp = inject('salarySettlementListApp', reactive({}));
    let salarySettlementGrid=inject("salarySettlementGrid",reactive({}));
    const salarySettlementModalApp = reactive({
      id: null,
      salarySettlement: {},
      isEdit: false,
      display: false
    });
    salarySettlementModalApp.rules = {
      staffName: [
        {
          required: false,
          type: 'string',
          message: '请输入员工',
          trigger: 'blur'
        }
      ],
      staffId: [
        {
          required: true,
          type: 'number',
          message: '请输入员工',
          trigger: 'blur'
        }
      ],
      amount: [
        {
          required: false,
          type: 'number',
          message: '请输入工资金额',
          trigger: 'blur'
        }
      ],
      settleStartDate: [
        {
          required: false,
          type: 'string',
          message: '请输入结算开始日期',
          trigger: 'blur'
        }
      ],
      settleEndDate: [
        {
          required: false,
          type: 'string',
          message: '请输入结算日期',
          trigger: 'blur'
        }
      ],
    }

    onMounted(() => {
      //清理数据
      salarySettlementModalApp.salarySettlement = reactive({});
      if (routeId) {//路由
        salarySettlementModalApp.id = routeId;
        salarySettlementModalApp.display = false;
        findSalarySettlement(salarySettlementModalApp.id);
      } else if (salarySettlementListApp.editModalForEdit) {//修改弹窗
        salarySettlementModalApp.id = salarySettlementListApp.currentId;
        salarySettlementModalApp.display = true;
      } else if (!salarySettlementListApp.editModalForEdit && salarySettlementListApp.addModalForEdit) {//新增弹窗
        salarySettlementModalApp.display = true;
      } else {
        salarySettlementModalApp.title = "新增学生";
      }
      if (salarySettlementModalApp.id) findSalarySettlement(salarySettlementModalApp.id);
    })

    const findSalarySettlement = (id) => {
      SalarySettlementService.findSalarySettlementForView(id).then((res) => {
        salarySettlementModalApp.salarySettlement = res.data;
        //TODO:为编辑准备.
        salarySettlementModalApp.title = "编辑学生 " + salarySettlementModalApp.salarySettlement.name;
      }).catch(error => {
        VXETable.modal.message({content: `查找学生出错，原因是：${error.message}`, status: 'error'});
      })
    }

    /*TODO:提交 新增&编辑*/
    const onSubmit = (data) => {
      // debugger;
      //当为数组时用逗号连接
      if (data.eid) {
        //修改
        SalarySettlementService.updateSalarySettlement(data).then((res) => {
          console.log(res);
          VXETable.modal.message({content: '操作成功', status: 'success'});
          setTimeout(onBack, 3000);
        }).catch(error => {
          VXETable.modal.message({content: `系统错误，原因是：${error.message}`, status: 'error'});
        })
      } else {
        //新增
        SalarySettlementService.saveSalarySettlement(data).then((res) => {
          console.log(res);
          VXETable.modal.message({content: '操作成功', status: 'success'});
          setTimeout(onBack, 3000);
        }).catch(error => {
          VXETable.modal.message({content: `系统错误，原因是：${error.message}`, status: 'error'});
        })
      }
    }

    /*TODO:继续提交*/
    const continueSubmit = () => {
      VXETable.modal.message({content: 'error 提示', status: 'error'});
    }

    /*TODO:返回列表*/
    const onBack = () => {
      router.push(`/restaurant/staff/salarySettlement`);
      store.dispatch('user/tabRemove', route.fullPath);
      setTimeout(() => {
        salarySettlementGrid.value.commitProxy("query");
      }, 1500);
      salarySettlementListApp.editModalShowing = false;
    }
    const calculateSalary=()=>{
      var staffId=salarySettlementModalApp.salarySettlement.staffId
      if(staffId==undefined||staffId==""){
        VXETable.modal.message({content: '请选择结算员工', status: 'warning'});
        return;
      }
      var settleStartDate=salarySettlementModalApp.salarySettlement.settleStartDate;
      if(settleStartDate==undefined||settleStartDate==""){
        VXETable.modal.message({content: '请选择结算开始时间', status: 'warning'});
        return;
      }
      var settleEndDate=salarySettlementModalApp.salarySettlement.settleEndDate
      if(settleEndDate==undefined||settleEndDate==""){
        VXETable.modal.message({content: '请选择结算时间', status: 'warning'});
        return;
      }
      SalarySettlementService.calculateSalary(staffId,settleStartDate,settleEndDate).then((res)=>{
        salarySettlementModalApp.salarySettlement.amount=res.data
      })

    }
    return {
      cityData,
      routeId,
      salarySettlementListApp,
      salarySettlementModalApp,
      onSubmit,
      continueSubmit,
      onBack,
      calculateSalary
    }

  },
})
</script>

<style scoped>

</style>
